//
//  WelcomeViewController.swift
//  SynWeibo
//
//  Created by 沈艳男 on 15/9/7.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    // 设置头像的地步约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        // 下载头像的图片
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 制作头像动画
        // 1、计算头像的约束
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        printLog(h)
        
        // 2、修改约束的数值
        iconBottomCons?.constant = h

        // 3、开始动画
        label.alpha = 0
        UIView.animateWithDuration(1.25, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        // 设置通知，通知AppDelegate 更换控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootViewControllerNotification, object: nil)
                })
        }
    }

    // MARK: - 懒加载控件
    // 背景图片
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 设置图片为圆形
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    // 文字
    
    private lazy var label: UILabel = {
        let ll = UILabel()
        ll.text = "欢迎回来"
        return ll
    }()
    
    // MARK: 设置控件的布局
    private func setUpUI() {
        // 1、添加控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        // 2、设置自动布局
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": backImageView]))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        // 记录头像的地步位置
        self.iconBottomCons = view.constraints.last
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))

        
        
        
    }
    
    

}
